<template>
  <div class="m-inspect-history">
    <el-button @click="back" class="m-floatRight">返回</el-button>
    <div class="m-agTable m-bottom">
      <ag-grid-vue
        class="m-agTableContent ag-theme-balham"
        :gridOptions="gridOptions"
        :columnDefs="columnDefs"
        :rowData="tableData"
        v-loading="loading"
        rowSelection="single"
      ></ag-grid-vue>
    </div>
    <!-- 页码 -->
    <div class="m-page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handleCurrentChange"
        @next-click="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[30, 50, 100, 150, 200]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import localeText from "@/i18n/agGridText";
import gridOptions from "lesso-common/public/gridOptions";
import _Api from "@/api";
import constant from "@/public/constant";

export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 30,
      total: 0,
      loading: false,
      tableData: [],
       // 用户类型,4为供应商，1为内部用户
      isInnerUser: this.$global.user.userTypeId == 1
    };
  },
  props: {
    queryParams: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  methods: {
    handleSizeChange(pageSize) {
      this.currentPage = 1;
      this.pageSize = pageSize;
      this.search();
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.search();
    },
    // 查询
    search() {
      let params = {
        current: this.currentPage,
        pageSize: this.pageSize,
        id: this.queryParams.id,
        supplierCode: !this.isInnerUser ? (constant.supplierData || {}).supplierCode : ''
      }
      _Api
        .queryRecord(params)
        .then(({ data }) => {
          this.loading = false;
          if (data.code === 1) {
            this.total = data.total;
            this.tableData = data.rows;
          } else {
            Msg.showErrorMsg(data.info);
          }
        })
        .catch(err => {
          this.loading = false;
          this.tableData = [];
          Msg.showErrorMsg(err.data.message);
        });
    },
    back() {
      this.$emit("back");
    }
  },
  mounted() {
    this.search();
  },
  beforeMount() {
    this.gridOptions = Object.assign(
      {
        context: {
          componentParent: this
        },
        localeText: localeText,
        suppressRowClickSelection: true,
        enableColResize: true,
        onGridReady: function($event) {}
      },
      gridOptions()
    );
    this.columnDefs = [
      {
        headerName: "检验单号",
        field: "recCode"
      },
      {
        headerName: "检验类型",
        field: "insType",
        cellRenderer: function(params) {
          const map = {
            0: '首检',
            1: '复检',
            2: '换货复检',
          }
          return map[params.value] || '';
        }
      },
      {
        headerName: "检验状态",
        field: "result",
        cellRenderer: function(params) {
          const map = {
            1: '合格',
          }
          return map[params.value] || '不合格';
        }
      },
      {
        headerName: "质检员",
        field: "insMan"
      },
      {
        headerName: "质检日期",
        field: "insTime"
      }
    ];
  }
};
</script>

